from abc import ABCMeta, abstractmethod


# 职责模式又称为责任链模式
# 譬如在公司中请假，如果是2天以内，部门领导有权力审批，2天到5天，还需分管领导，5天以上，提交行政部门
# 这样每一级可以动态判断自己是否可以审批，如果没有权力，就一级一级往上报
class Request(metaclass=ABCMeta):
    """请求（内容）"""
    def __init__(self, name, dayoff, reason):
        self._name = name
        self._dayoff = dayoff
        self._reason = reason
        self._leader = None

    def getName(self):
        return self._name

    def setName(self, name):
        self._name = name

    def getDayoff(self):
        return self._dayoff

    def setDayoff(self, dayoff):
        self._dayoff = dayoff
    

    def getLeader(self):
        return self._leader

    def setLeader(self, leader):
        self._leader = leader

    def sendRequest(self):
        print(f"{self._name}因为{self._reason}申请请假{self._dayoff}天")
        if self._leader is not None:
            self._leader.handleRequest(self)
        else:
            print("您最大，不用向别人请假")


class Responsible(metaclass=ABCMeta):
    """责任人抽象"""
    def __init__(self, name, title):
        self._name = name
        self._title = title
        self._nextLeader = None

    def getName(self):
        return self._name

    def getTitle(self):
        return self._title

    def setNextLeader(self, nextLeader):
        self._nextLeader = nextLeader

    def getNextLeader(self):
        return self._nextLeader

    def handleRequest(self, request):
        """请求处理"""
        # 当前责任人处理请求
        self._handleRequestImpl(request)
        if self._nextLeader is not None:
            self._nextLeader.handleRequest(request)
    
    @abstractmethod
    def _handleRequestImpl(self, request):
        """真正处理请求的方法"""
        pass


class Person(Request):
    pass


class Supervisor(Responsible):
    """主管"""
    def __init__(self, name, title):
        super().__init__(name, title)

    def _handleRequestImpl(self, request):
        if(request.getDayoff() < 2):
            print(f"同意{request.getName()}请假，签字人：{self.getName()}({self.getTitle()})")


class DepartmentManager(Responsible):
    """部门总监"""
    def __init__(self, name, title):
        super().__init__(name, title)

    def _handleRequestImpl(self, request):
        if(request.getDayoff() > 2 and request.getDayoff() < 5):
            print(f"同意{request.getName()}请假，签字人：{self.getName()}({self.getTitle()})")


class CEO(Responsible):
    """CEO"""
    def __init__(self, name, title):
        super().__init__(name, title)

    def _handleRequestImpl(self, request):
        if(request.getDayoff() > 5 and request.getDayoff() < 22):
            print(f"同意{request.getName()}请假，签字人：{self.getName()}({self.getTitle()})")


class Adminstrator(Responsible):
    """行政人员"""
    def __init__(self, name, title):
        super().__init__(name, title)

    def _handleRequestImpl(self, request):
       print(f"{self.getName()}的请假申请已审核，情况属实！已备案处理。处理人：{self.getName()}({self.getTitle()})")


def test():
    directorLeader = Supervisor("Eren", "客户端研发部经理")

    departmentmanager = DepartmentManager("Eric", "技术研发中心总监")
    directorLeader.setNextLeader(departmentmanager)
    ceo = CEO("Helen", "创新文化公司CEO")
    departmentmanager.setNextLeader(ceo)
    adminstrator = Adminstrator("Nina", "行政中心总监")
    ceo.setNextLeader(adminstrator)

    will = Person("Will", 1, "参加MDCC大会")
    will.setLeader(directorLeader)
    will.sendRequest()

    tom = Person("Tom", 20, "结婚")
    tom.setLeader(directorLeader)
    tom.sendRequest()


if __name__ == "__main__":
    test()






    


